package com.cdp.zwy.zwy_manager_back_v1.service;

import cn.hutool.core.util.StrUtil;
import com.cdp.zwy.zwy_manager_back_v1.common.ServiceException;
import com.cdp.zwy.zwy_manager_back_v1.common.ErrorCode;
import com.cdp.zwy.zwy_manager_back_v1.entity.Account;
import com.cdp.zwy.zwy_manager_back_v1.entity.Admin;
import com.cdp.zwy.zwy_manager_back_v1.mapper.AdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zwy
 * @version 1.0
 * @description: AdminService
 * @date 2025/4/14 19:05
 */
@Service
public class AdminService {
    @Resource
    private AdminMapper adminMapper;
    public int add(Admin admin) {
        if(StrUtil.isBlank(admin.getName())){
            admin.setName("默认名称");
        }
        if(StrUtil.isBlank(admin.getRole())){
            admin.setRole("ADMIN");
        }
        int row =  adminMapper.insert(admin);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_ADD_ERROR);
        }
        return row;
    }
    public int deleteById(Integer id) {
        Admin admin = adminMapper.selectById(id);
        if (admin == null){
            throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
        }
        int row =  adminMapper.deleteById(id);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_DELETE_ERROR);
        }
        return row;
    }
    public int modify(Admin admin) {
        Admin admin1 = adminMapper.selectById(admin.getId());
        if (admin1 == null){
            throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
        }
        int row =  adminMapper.update(admin);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_MODIFY_ERROR);
        }
        return row;
    }
    public Admin findById(Integer id) {
        Admin admin = adminMapper.selectById(id);
        if(admin == null){
            throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
        }
        return admin;
    }
    public List<Admin> findAll() {
        List<Admin> admins = adminMapper.selectAll();
        if(admins.size() == 0){
            throw new ServiceException(ErrorCode.NOT_DATA);
        }
        return admins;
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Admin> selectPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> list = adminMapper.selectAll();

        return PageInfo.of(list);
    }

    public PageInfo<Admin> findByName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> list = adminMapper.selectFuzzy(name);

        return PageInfo.of(list);
    }

    public int deleteBatch(List<Integer> ids) {
        int rows = 0;
        for (Integer id : ids) {
            Admin admin = adminMapper.selectById(id);
            if (admin == null){
                throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
            }
            int row =  adminMapper.deleteById(id);
            if(row!=1){
                throw new ServiceException(ErrorCode.EMPLOYEE_DELETE_ERROR);
            }
            rows++;
        }
        return rows;
    }

    public Admin login(Account admin) {
        Admin findAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (findAdmin == null){
            throw new ServiceException(ErrorCode.ADMIN_NOT_FOUND);
        }
        if (!findAdmin.getPassword().equals(admin.getPassword())){
            throw new ServiceException(ErrorCode.ADMIN_LOGIN_ERROR);
        }
        return findAdmin;
    }
    public Admin register(Admin admin){
        Admin findAdmin = adminMapper.selectByUsername(admin.getUsername());
        admin.setRole("ADMIN");
        admin.setName("默认名称");



        if (findAdmin != null){
            throw new ServiceException(ErrorCode.EMPLOYEE_REGISTER_ERROR_USERNAME);
        }
        int row =  adminMapper.insert(admin);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_ADD_ERROR);
        }
        return admin;
    }
    public Admin modifyPassword(Account admin) {
        Admin findAdmin = adminMapper.selectById(admin.getId());
        if (findAdmin == null){
            throw new ServiceException(ErrorCode.ADMIN_NOT_FOUND);
        }
        if (!findAdmin.getPassword().equals(admin.getOldPassword())){
            throw new ServiceException(ErrorCode.ADMIN_OLDPASSWORD_ERROR);
        }
        findAdmin.setPassword(admin.getPassword());
        int row =  adminMapper.update(findAdmin);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_MODIFY_ERROR);
        }
        return findAdmin;
    }
}
